import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;


public class chatserverUDP {
	public static void main(String[] args) throws IOException{
		//cong lang nghe
		DatagramSocket serverSK=new DatagramSocket(8888);
		String tinnhan;
		do
		{	//nhan tin nhan tu client
			byte []buf=new byte[1024];
			DatagramPacket serverPK=new DatagramPacket(buf, buf.length);
			serverSK.receive(serverPK);
			tinnhan=new String(serverPK.getData());
			System.out.println("CLient: "+tinnhan.trim());
			if(tinnhan.trim().equalsIgnoreCase("exit"))
			{
				serverPK=new DatagramPacket("exit".getBytes(), "exit".getBytes().length, serverPK.getAddress(), serverPK.getPort());
				serverSK.send(serverPK);
				break;
			}
			Scanner input =new Scanner(System.in);
			tinnhan=input.nextLine();
			System.out.println("Server: ");
			
			byte[]data=tinnhan.getBytes();
			serverPK=new DatagramPacket(data, data.length, serverPK.getAddress(), serverPK.getPort());
			serverSK.send(serverPK);
			input.close();
			
		}
		while(!tinnhan.trim().equalsIgnoreCase("exit"));
		serverSK.close();
	}
}
